package cn.zwk.drawing.application.controller;

import cn.zwk.drawing.domain.configuration.service.ConfigurationDomainService;
import cn.zwk.drawing.domain.diagram.model.DiagramDefinition;
import cn.zwk.drawing.application.dto.ConfigurationResponse;
import cn.zwk.drawing.application.dto.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;

/**
 * 配置管理API控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/configuration")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class ConfigurationController {

    private final ConfigurationDomainService configurationService;

    /**
     * 获取架构图配置
     */
    @GetMapping
    public ResponseEntity<ApiResponse<ConfigurationResponse>> getConfiguration() {
        log.info("获取架构图配置");

        try {
            Optional<DiagramDefinition> diagram = configurationService.getDiagramConfiguration();

            if (diagram.isEmpty()) {
                return ResponseEntity.ok(ApiResponse.error("未找到配置信息"));
            }

            DiagramDefinition def = diagram.get();
            ConfigurationResponse response = ConfigurationResponse.builder()
                    .diagramId(def.getDiagramId())
                    .name(def.getName())
                    .plantUmlContent(def.getPlantUmlContent())
                    .version(def.getVersion())
                    .lastModified(def.getLastModified())
                    .status("success")
                    .build();

            return ResponseEntity.ok(ApiResponse.success(response));

        } catch (Exception e) {
            log.error("获取配置失败", e);
            return ResponseEntity.ok(ApiResponse.error("获取配置失败: " + e.getMessage()));
        }
    }

    /**
     * 重新加载配置
     */
    @PostMapping("/reload")
    public ResponseEntity<ApiResponse<String>> reloadConfiguration() {
        log.info("重新加载配置");

        try {
            configurationService.reloadConfigurations();
            return ResponseEntity.ok(ApiResponse.success("配置重新加载成功"));

        } catch (Exception e) {
            log.error("重新加载配置失败", e);
            return ResponseEntity.ok(ApiResponse.error("重新加载失败: " + e.getMessage()));
        }
    }

    /**
     * 获取配置状态
     */
    @GetMapping("/status")
    public ResponseEntity<ApiResponse<Object>> getConfigurationStatus() {
        log.debug("获取配置状态");

        try {
            Map<String, Object> statistics = Map.of(
                "timestamp", LocalDateTime.now(),
                "configurationCount", configurationService.getConfigurationsByType(null).size(),
                "status", "healthy"
            );

            return ResponseEntity.ok(ApiResponse.success(statistics));

        } catch (Exception e) {
            log.error("获取配置状态失败", e);
            return ResponseEntity.ok(ApiResponse.error("获取状态失败: " + e.getMessage()));
        }
    }
}
